//
//  TabBarController.m
//  XLNews
//
//  Created by 薛林 on 16/12/19.
//  Copyright © 2016年 YunTianXia. All rights reserved.
//

#import "TabBarController.h"
#import <SDImageCache.h>
#import "NavigationController.h"
#import <DKNightVersion/DKNightVersion.h>
#import "MeTableViewController.h"
#import "NewsViewController.h"
#import "Const.h"
#import "PictureViewController.h"
#import "VideoViewController.h"

@interface TabBarController ()<MeTableViewControllerDelegate> {
    MeTableViewController *_MeController;
}

@property (nonatomic, assign) BOOL isShakeCanChangeSkin;

@end

@implementation TabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NewsViewController *vc1 = [[NewsViewController alloc] init];
    [self addChildViewController:vc1 withImage:[UIImage imageNamed:@"tabbar_news"] selectedImage:[UIImage imageNamed:@"tabbar_news_hl"] withTittle:@"新闻"];
    
    PictureViewController *vc2 = [[PictureViewController alloc] init];
    [self addChildViewController:vc2 withImage:[UIImage imageNamed:@"tabbar_picture"] selectedImage:[UIImage imageNamed:@"tabbar_picture_hl"] withTittle:@"图片"];
    
    VideoViewController *vc3 = [[VideoViewController alloc] init];
    [self addChildViewController:vc3 withImage:[UIImage imageNamed:@"tabbar_video"] selectedImage:[UIImage imageNamed:@"tabbar_video_hl"] withTittle:@"视频"];
    
    MeTableViewController *vc4 = [[MeTableViewController alloc] init];
    _MeController = vc4;
    [self addChildViewController:vc4 withImage:[UIImage imageNamed:@"tabbar_setting"] selectedImage:[UIImage imageNamed:@"tabbar_setting_hl"] withTittle:@"我的"];
    vc4.delegate = self;
    
    [self setupBasic];
    
    
    
}

- (void)setupBasic  {
    if ([self.dk_manager.themeVersion isEqualToString:DKThemeVersionNormal] ) {
        self.tabBar.barTintColor = [UIColor whiteColor];
        
    } else {
        self.tabBar.barTintColor = [UIColor colorWithRed:34/255.0 green:34/255.0 blue:34/255.0 alpha:1.0];
    }
    //晃动是否有撤销或者重做动作
    [UIApplication sharedApplication].applicationSupportsShakeToEdit = YES;
    [self becomeFirstResponder];
    self.isShakeCanChangeSkin = [[NSUserDefaults standardUserDefaults] boolForKey:IsShakeCanChangeSkinKey];
    
}


//创建子控制器
- (void)addChildViewController:(UIViewController *)controller withImage:(UIImage *)image selectedImage:(UIImage *)selectImage withTittle:(NSString *)tittle{
    NavigationController *nav = [[NavigationController alloc] initWithRootViewController:controller];
    
    [nav.tabBarItem setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [nav.tabBarItem setSelectedImage:[selectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    [nav.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateSelected];
    
    controller.title = tittle;
    //调整tabBarItem上文字的位置
    nav.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -3);
    [self addChildViewController:nav];
}
//摇一摇换肤
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (motion == UIEventSubtypeMotionShake) {
        if (self.isShakeCanChangeSkin == NO ) return;
        if ([self.dk_manager.themeVersion isEqualToString:DKThemeVersionNormal]) {
            self.dk_manager.themeVersion = DKThemeVersionNight;
            self.tabBar.barTintColor = [UIColor colorWithRed:34/255.0 green:34/255.0 blue:34/255.0 alpha:1.0];
            _MeController.changeSkinSwitch.on = YES;
            
        } else {
            self.dk_manager.themeVersion = DKThemeVersionNormal;
            self.tabBar.barTintColor = [UIColor whiteColor];
            _MeController.changeSkinSwitch.on = NO;
        }
        
    }
    
    
}

-(void)didReceiveMemoryWarning {
    [[SDImageCache sharedImageCache] clearDisk];
    
}

@end
